<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="模型名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          placeholder="请输入模型名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布状态" prop="isrelease">
        <el-select v-model="queryParams.isrelease" placeholder="请选择发布状态">
            <el-option label="全部" value="" />
            <el-option
              v-for="dict in isreleases"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bid:model:add']"
        >新增</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bid:model:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bid:model:remove']"
        >删除</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="modelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模型名称" align="center" prop="modelName" />
      <el-table-column label="描述" align="center" prop="modelMsg" />
      <el-table-column label="发布状态" align="center" prop="isrelease" :formatter="isreleaseFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isrelease == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bid:model:edit']"
          >修改</el-button>
          <el-button v-if="scope.row.isrelease == 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bid:model:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['bid:model:edit']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模型管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="描述" prop="modelMsg">
          <el-input v-model="form.modelMsg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="发布状态" prop="isrelease">
          <el-select v-model="form.isrelease" placeholder="请选择发布状态">
            <el-option
              v-for="dict in isreleases"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">模型字段信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCrmBidModelColumn">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCrmBidModelColumn">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="crmBidModelColumnList" :row-class-name="rowCrmBidModelColumnIndex" @selection-change="handleCrmBidModelColumnSelectionChange" ref="crmBidModelColumn">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="字段名称" prop="modelColumn">
            <template slot-scope="scope">
              <el-input v-model="scope.row.modelColumn" placeholder="请输入英文字段名称" />
            </template>
          </el-table-column>
          <!--<el-table-column label="字段类型" prop="modelColumnType">
            <template slot-scope="scope">
              <el-input v-model="scope.row.modelColumnType" placeholder="请输入字段类型" />
            </template>
          </el-table-column>-->
          <el-table-column label="字段说明" prop="modelColumnMsg">
            <template slot-scope="scope">
              <el-input v-model="scope.row.modelColumnMsg" placeholder="请输入字段说明" />
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="order">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.order" controls-position="right" :min="0"></el-input-number>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 模型详情对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="form.modelName" readonly="true" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="描述" prop="modelMsg">
          <el-input v-model="form.modelMsg" type="textarea" readonly="true" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="发布状态" prop="isrelease">
          <el-select v-model="form.isrelease" disabled placeholder="请选择发布状态">
            <el-option
              v-for="dict in isreleases"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">模型字段信息</el-divider>
        <el-table :data="crmBidModelColumnList" :row-class-name="rowCrmBidModelColumnIndex" @selection-change="handleCrmBidModelColumnSelectionChange" ref="crmBidModelColumn">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="字段名称" prop="modelColumn">
            <template slot-scope="scope">
              <el-input v-model="scope.row.modelColumn" readonly="true" placeholder="请输入英文字段名称" />
            </template>
          </el-table-column>
          <!--<el-table-column label="字段类型" prop="modelColumnType">
            <template slot-scope="scope">
              <el-input v-model="scope.row.modelColumnType" placeholder="请输入字段类型" />
            </template>
          </el-table-column>-->
          <el-table-column label="字段说明" prop="modelColumnMsg">
            <template slot-scope="scope">
              <el-input v-model="scope.row.modelColumnMsg" readonly="true" placeholder="请输入字段说明" />
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="order">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.order" controls-position="right" :min="0"></el-input-number>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listModel, getModel, delModel, addModel, updateModel, exportModel } from "@/api/crm/bid/model";

export default {
  name: "Model",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedCrmBidModelColumn: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模型管理表格数据
      modelList: [],
      // 模型字段表格数据
      crmBidModelColumnList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title1: "",
      // 是否显示弹出层
      open1: false,
      //发布状态
      isreleases:[{dictValue: 0,dictLabel: '未发布'},{dictValue: 1,dictLabel: '发布'}],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        modelName: [
          { required: true, message: "模型名称不能为空", trigger: "blur" }
        ],
        isrelease: [
          { required: true, message: "发布状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模型管理列表 */
    getList() {
      this.loading = true;
      listModel(this.queryParams).then(response => {
        this.modelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        modelName: null,
        modelMsg: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.crmBidModelColumnList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getModel(id).then(response => {
        this.form = response.data;
        this.crmBidModelColumnList = response.data.crmBidModelColumnList;
        this.open = true;
        this.title = "修改模型";
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getModel(id).then(response => {
        this.form = response.data;
        this.crmBidModelColumnList = response.data.crmBidModelColumnList;
        this.open1 = true;
        this.title1 = "模型详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.crmBidModelColumnList = this.crmBidModelColumnList;
          if (this.form.id != null) {
            updateModel(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.open1 = false;
              this.getList();
            });
          } else {
            addModel(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除模型编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delModel(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
	/** 模型字段序号 */
    rowCrmBidModelColumnIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 模型字段添加按钮操作 */
    handleAddCrmBidModelColumn() {
      let obj = {};
      obj.modelColumn = "";
      obj.modelColumnType = "";
      obj.modelColumnMsg = "";
      this.crmBidModelColumnList.push(obj);
    },
    /** 模型字段删除按钮操作 */
    handleDeleteCrmBidModelColumn() {
      if (this.checkedCrmBidModelColumn.length == 0) {
        this.$alert("请先选择要删除的模型字段数据", "提示", { confirmButtonText: "确定", });
      } else {
        this.crmBidModelColumnList.splice(this.checkedCrmBidModelColumn[0].index - 1, 1);
      }
    },
    /** 单选框选中数据 */
    handleCrmBidModelColumnSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.crmBidModelColumn.clearSelection();
        this.$refs.crmBidModelColumn.toggleRowSelection(selection.pop());
      } else {
        this.checkedCrmBidModelColumn = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有模型管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportModel(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    // 发布状态翻译
    isreleaseFormat(row, column) {
      return this.selectDictLabel(this.isreleases, row.isrelease);
    }
  }
};
</script>
